package simple.utils.data.caches;

import com.google.common.cache.Cache;
import simple.utils.data.KeyValueCache;

/**
 * Created by rehak on 5/13/14.
 */
public class GoogleCache<E, V> implements KeyValueCache<E, V>, AutoCloseable {

    private final Cache<E, V> cache;

    public GoogleCache(Cache<E, V> cache) {
        this.cache = cache;
    }

    @Override
    public void put(E key, V val) {
        cache.put(key, val);
    }

    @Override
    public V get(E key) {
        return cache.getIfPresent(key);
    }

    @Override
    public void delete(E key) {
        cache.invalidate(key);
    }

    @Override
    public void close() {
        cache.invalidateAll();
        cache.cleanUp();
    }
}
